const API = require('../../common/constant');
const util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    event: 'true',
    events: [],
    joinedEvents: [],
    pageIndexRequest: 1,
    pageCountRequest: 0,
    pageIndexJoined: 1,
    pageCountJoined: 0,
  },
  toEventManage: function (e) {
    wx.navigateTo({
      url: '../FanInfo/FanInfo?id=' + e.currentTarget.id
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getEvents();
    this.getJoinedEvents();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 筛选出报名申请的活动
   */
  shaixuanData: function (data) {
    let newData = [];
    for (let i = 0; i < data.length; i++) {
      if (data[i].registrationStatus == "等待确认") {
        newData.push(data[i]);
      }
    }
    console.log('筛选后结果为', newData)
    return newData;
  },
  /**
   * 获取报名的活动
   */
  getEvents: function () {
    util.http({
      url: API.URL.REGISTRATION.FIND_EVENTREGISTRATION,
      data: {
        pageIndex: this.data.pageIndexRequest,
        pageSize: 8
      },
      success: res => {
        console.log('报名信息为:', res);
        let newData = this.changeData(res.data.bussData);
        newData = this.shaixuanData(newData);
        this.setData({
          events: this.data.events.concat(newData),
          pageCountRequest: res.data.pageCount
        })
      },
      fail: rej => {
        console.log(rej);
      }
    });
  },
  /**
   * 修改数据
   */
  changeData: function (data) {
    for (let i = 0; i < data.length; i++) {
      if (data[i].eventType === "chat") {
        data[i].activityType = "#解闷聊天#";
        data[i].activityColor = '#F4BC11';
      } else if (data[i].eventType === "partner") {
        data[i].activityType = "#美食搭伙#";
        data[i].activityColor = '#FF8023';
      } else if (data[i].eventType === 'find') {
        data[i].activityType = "#求邂逅#";
        data[i].activityColor = '#FE3232';
      }
    }
    console.log(data);
    return data;
  },
  /**
   * 获取已经报名的活动
   */
  getJoinedEvents: function () {
    util.http({
      url: API.URL.REGISTRATION.FIND_ALREADYEVENTREGISTRATION,
      data: {
        pageIndex: this.data.pageIndexJoined,
        pageSize: 8
      },
      success: res => {
        console.log("获取已报名", res)
        let newData = this.changeData(res.data.bussData);
        this.setData({
          joinedEvents: this.data.joinedEvents.concat(newData),
          pageCountJoined: res.data.pageCount
        })
      },
      fail: rej => {
        console.log(rej);
      }
    })
  },
  /**
   * 选择报名的
   */
  GetEventRequest: function (e) {
    this.setData({
      event: 'true'
    })
  },
  /**
   * 选择已报名的
   */
  GetJoinedEvent: function (e) {
    this.setData({
      event: 'false'
    })
  },
  onReachBottom() {
    // event为true时在报名页
    if(this.data.event === 'true'){
      if (this.data.pageCountRequest <= this.data.pageIndexRequest) return
      this.setData({
        pageIndexRequest: this.data.pageIndexRequest + 1
      })
      this.getEvents();
    }
    else{
      if (this.data.pageCountJoined <= this.data.pageIndexJoined) return
      this.setData({
        pageIndexJoined: this.data.pageIndexJoined + 1
      })
      this.getJoinedEvents();
    }
  }
})